package com.franklin.ideaplugin.mybatisrunner.linemarker;

import com.franklin.ideaplugin.api.linemarker.AbstractLineMarkerProvider;
import com.franklin.ideaplugin.mybatisrunner.annotations.MybatisAnnotations;
import com.franklin.ideaplugin.mybatisrunner.icon.PluginIcons;
import com.franklin.ideaplugin.mybatisrunner.xml.tag.MapperXmlTags;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import org.apache.commons.lang3.BooleanUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author Ye Junhui
 * @since 2024/2/19
 */
public class MapperXmlLineMarker extends AbstractLineMarkerProvider<XmlTag> {
    @Override
    protected boolean isMarkerTarget(@NotNull PsiElement psiElement) {
        PsiFile containingFile = psiElement.getContainingFile();
        //mapper
        if (BooleanUtils.isNotTrue(containingFile instanceof XmlFile)){
            return false;
        }

        //注解
        if (BooleanUtils.isNotTrue(psiElement instanceof XmlTag)){
            return false;
        }

        XmlTag xmlTag = (XmlTag) psiElement;

        //mybatis-xml标签注解
        return Arrays.stream(MapperXmlTags.values())
                .anyMatch(mapperXmlTag -> mapperXmlTag.name().equalsIgnoreCase(xmlTag.getName()));
    }

    @Override
    public @NotNull Icon getIcon() {
        return PluginIcons.PLUGIN;
    }

    @Override
    protected XmlTag getObject(PsiElement psiElement) {
        if (!isMarkerTarget(psiElement)){
            return null;
        }
        return (XmlTag) psiElement;
    }

    @Override
    public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
        if (!isMarkerTarget(element)){
            return null;
        }
        XmlTag xmlTag = getObject(element);
        return new MapperXmlLineMarkerInfo(
                xmlTag,
                getIcon(),
                (Navigatable) xmlTag.getNavigationElement(),
                Collections.singleton("")
        );
    }
}
